package com.lzj.server.utils;

import jakarta.annotation.PostConstruct;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    private final RedisTemplate<String, Object> redisTemplate;

    // 静态实例，用于静态方法中访问 RedisTemplate
    private static RedisTemplate<String, Object> staticRedisTemplate;

    public RedisUtils(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    public void init() {
        staticRedisTemplate = this.redisTemplate;
    }

    /**
     * 写入缓存
     */
    public static void set(String key, Object value) {
        staticRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 有序集合写入缓存
     */
    public static void zSet(String key, Object value, double score) {
        staticRedisTemplate.opsForZSet().add(key, value, score);
    }

    /**
     * 有序集合获取全部数据
     */
    public static Set<Object> zRange(String key) {
        return staticRedisTemplate.opsForZSet().range(key, 0, -1);
    }

    /**
     * 获取有序集合中的所有元素和 score（带顺序）
     */
    public static Set<ZSetOperations.TypedTuple<Object>> zRangeWithScores(String key, int start, int end) {
        return staticRedisTemplate.opsForZSet().rangeWithScores(key, start, end);
    }

    /**
     * 写入缓存并设置过期时间（秒）
     */
    public static void set(String key, Object value, long timeoutSeconds) {
        staticRedisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS);
    }

    /**
     * 写入缓存并设置过期时间（手动指定时间单位）
     */
    public static void set(String key, Object value, long timeoutSeconds, TimeUnit timeUnit) {
        staticRedisTemplate.opsForValue().set(key, value, timeoutSeconds, timeUnit);
    }

    /**
     * 读取缓存
     */
    public static Object get(String key) {
        return staticRedisTemplate.opsForValue().get(key);
    }

    /**
     * 自动转换对象类型
     */
    @SuppressWarnings("unchecked")
    public static <T> T getObjectBean(String key, Class<T> clazz) {
        Object obj = get(key);
        if (clazz.isInstance(obj)) {
            return (T) obj;
        }
        return null;
    }

    /**
     * 删除缓存
     */
    public static void del(String key) {
        staticRedisTemplate.delete(key);
    }

    /**
     * 是否存在 key
     */
    public static boolean hasKey(String key) {
        Boolean result = staticRedisTemplate.hasKey(key);
        return Boolean.TRUE.equals(result);
    }

    /**
     * 设置过期时间（秒）
     */
    public static void expire(String key, long timeoutSeconds) {
        staticRedisTemplate.expire(key, timeoutSeconds, TimeUnit.SECONDS);
    }

    /**
     * 获取剩余过期时间（秒）
     */
    public static Long getExpire(String key) {
        return staticRedisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 判断 key 是否存在
     */
    public static boolean exist(String key) {
        return Boolean.TRUE.equals(staticRedisTemplate.hasKey(key));
    }
}
